<?php
/**
 * XPHPFramework
 *
 * XRequest class, XArrayObject class, XArrayElementObject class, XCookieObject class,
 * XCookieElementObject class,XSessionObject class
 *
 * PHP version 5.3
 * 
 * @category XRequest
 * @package XPHPFramework
 * @author chopins xiao <chopins.xiao@gmail.com>
 * @copyright  2012 The Authors
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 * @link       http://blog.toknot.com
 * @since      File available since Release 2.2
 */

exists_frame();

/**
 * session array object
 *
 * @package XPHPFramework
 * @author chopins xiao <chopins.xiao@gmail.com>
 */
class XSessionObject extends XArrayObject {
    public $cfg = null;
    public $sid = null;
    public function __construct() {
        global $_CFG;
        $this->cfg = $_CFG;
        $this->startSession();
        $this->initArray();
    }
    public function initArray() {
        parent::__construct($_SESSION);
    } 
    public function startSession() {
        if(!file_exists(__X_APP_DATA_DIR__."/{$this->cfg->session->save_path}")) {
            mkdir(__X_APP_DATA_DIR__."/{$this->cfg->session->save_path}");
        }
        session_name($this->cfg->session->session_name);
        session_save_path(__X_APP_DATA_DIR__."/{$this->cfg->session->save_path}");
        session_start();
        $this->sid = session_id();
    }
    /**
     * set array object element value at a specified any type index
     */
    public function offsetSet($sKey, $value) {
        parent::offsetSet($sKey,$value);
        $_SESSION[$sKey] = $value;
    }
    public function offsetUnset($sKey) {
        parent::offsetUnset($sKey);
        unset($_SESSION[$sKey]);
    }
}

/**
 * one cookie object
 *
 * @package XPHPFramework
 * @author chopins xiao <chopins.xiao@gmail.com>
 */
class XCookieElementObject {//extends XArrayElementObject {
    /**
     * set cookie expire, default value is 0,
     * @var int
     * @access public
     */
    public $expire = 0;

    /**
     * set cookie available for this domain
     * @var string
     * @access public
     */
    public $domain = '';

    /**
     * set cookie accessible only through the HTTP protocol
     * @var bool
     * @access public
     */
    public $httponly = false;

    /**
     * set cookie only be transmitted over a secure HTTPS
     * @var bool
     * @access public
     */
    public $secure = false;

    /**
     * set cookie be available on path
     * @var string
     * @access public
     */
    public $path = '/';
    public $name = '';
    public $value = '';
    public function __construct($value, $name) {
        $this->value = $value;
        $this->name = $name;
    }
    /**
     * set cookie value
     */
    public function set() {
        setcookie($this->name,$this->value,
                $this->expire,$this->path,$this->domain,
                $this->secure,$this->httponly);
    }
}

/**
 * cookie array object
 *
 * @package XPHPFramework
 * @author chopins xiao <chopins.xiao@gmail.com>
 */
class XCookieObject extends XArrayObject {
    public $num = 0;
    protected $elementObjectName ='XCookieElementObject';
    public function __set($sKey, $value) {
        parent::__set($sKey,$value);
        $this->num ++;
    }
}

/**
 * base request operation class
 *
 * @package XPHPFramework
 * @author chopins xiao <chopins.xiao@gmail.com>
 */
class XRequest {
    /**
     * $_GET data object storage
     *
     * @var object
     * @access public-readonly
     */
    private $G = null;

    /**
     * $_POST data object storage
     *
     * @var object
     * @access public-readonly
     */
    private $P = null;
    
    /**
     * cookie data storage and operation class instance
     *
     * @var object
     * @access public-readonly
     */
    private $C = null;

    /**
     * session data storage and operation class instance
     *
     * @var object
     * @access public-readonly
     */
    private $S = null;

    /**
     * POST JSON data storage class instance
     *
     * @var object
     * @access public-readonly
     */
    private $A = null;

    /**
     * current user visit page method
     * @var string
     * @access public-readonly
     */
    private $M = 'GET';
   
    /**
     * Request Data storage class instance
     *
     * @var object
     * @access public-readonly
     */
    private $_R = null;

    /**
     * construct request data structure
     */
    public function __construct() {
        $this->M = $_SERVER['REQUEST_METHOD'];
        $this->G = new XArrayObject($_GET);
        $this->P = new XArrayObject($_POST);
        $this->C = new XCookieObject($_COOKIE);
        $this->_R = new XArrayObject($_REQUEST);
    }
    public function __get($pro) {
        switch($pro) {
            case 'S':
            if(!is_object($this->S)) $this->initSession();
            break;
            case 'A':
            $this->getAjaxData();
            break;
            case '_R':
            throw new XException('XRequest class $_R propertie is private');
            return;
        }
        if(isset($this->$pro)) return $this->$pro;
        if(isset($this->_R->$pro)) return $this->_R->$pro;
    }
    public function __set($pro, $value) {
        $this->_R->$pro = $value;
    }
    public function initSession() {
        $this->S = new XSessionObject();
    }
    public function getAjaxData() {
        global $_CFG;
        if(isset($_REQUEST[$_CFG->ajax_key]) && isset($_REQUEST[$_CFG->ajax_flag])) {
            $ajax_data_array = json_decode($_REQUEST[$_CFG->ajax_key]);
            if(json_last_error() == JSON_ERROR_NONE) {
                return $this->A = $ajax_data_array;
            }
            switch(json_last_error()) {
                case JSON_ERROR_DEPTH:
                    $error = 'The maximum stack depth has been exceeded';
                break;
                case JSON_ERROR_STATE_MISMATCH:
                    $error = 'Invalid or malformed JSON';
                break;
                case JSON_ERROR_CTRL_CHAR:
                    $error = 'Control character error, possibly incorrectly encoded';
                break;
                case JSON_ERROR_SYNTAX:
                    $error = '  Syntax error';
                break;
                case JSON_ERROR_UTF8:
                    $error ='Malformed UTF-8 characters, possibly incorrectly encoded';
                break;
            }
            throw new XException("request ajax data decode error,$error");
        }
    }
}



